/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cardarus.operatingsystem.struts.actions;

import com.cardarus.operatingsystem.struts.forms.RegisterRemindInputForm;
import com.cardarus.operatingsystem.model.BusinessModel;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.cardarus.operatingsystem.domain.Customer;
import com.cardarus.operatingsystem.domain.Item;
import com.cardarus.operatingsystem.domain.SalesTransaction;
import com.cardarus.operatingsystem.domain.SalesTransactionRow;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * This class responds to confirming Customer's registration of Remind plan.
 * This class will display concerning information to Customer and
 * let them confirm those information.
 * @author Daqi
 */
public class RegisterRemindConfirmAction extends org.apache.struts.action.Action {

    private static final int REGISTER_REWARD_ITEM = 1;

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        RegisterRemindInputForm formBean = (RegisterRemindInputForm) form;
        BusinessModel model = BusinessModel.getInstance();

        boolean newUser = formBean.isNewUser();
        Customer user = null;
        String pin;

        if (newUser) {
            user = new Customer();
            // set user data
            user.setAddress(formBean.getAddress());
            user.setEmail(formBean.getEmail());
            user.setFamilyName(formBean.getFamilyName());
            user.setName(formBean.getName());
            user.setBirthday(formBean.getBirthday());
            user.setPassword(formBean.getPassword());
            user.setPhone(formBean.getPhone());
            user.setUserName(formBean.getUserName());

            //Create new Customer
            if (model.createNewCustomer(user)) {
                pin = user.getPin();
                formBean.setPin(pin);
                user = model.getCustomerByPin(pin);
            } else {
                return mapping.findForward("error");
            }
        } else {
            pin = formBean.getPin();
            user = model.getCustomerByPin(pin);
        }
        HttpSession session = request.getSession();

        SalesTransaction sale = (SalesTransaction) session.getAttribute("sale");



        if (model.registerRemind(user)) {
            if (sale != null) {
                List<SalesTransactionRow> rows = sale.getRows();

                Item item = model.getItem(REGISTER_REWARD_ITEM);

                SalesTransactionRow row = new SalesTransactionRow(0, rows.size(), item, 1, item.getPrice(), item.getPrice());
                rows.add(row);

                sale.setSubTotal(sale.getSubTotal() + item.getPrice());
                model.calculateTotal(sale);
            }
            return mapping.findForward("success");
        } else {
            // TODO: Error of that Customer has registered this Reward plan.
            return mapping.findForward("error");
        }
    }
}
